/*--------------------------------------------------|
| basic 1.00 |  lovefuwp                            |
| Updated: 2009-9-10                                |
|--------------------------------------------------*/

function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

//change color
function tableList() {
	if(!document.getElementsByTagName("table")) return false;
	var tableL=document.getElementsByTagName("table");
	for(i=0;i<tableL.length;i++){
		var tableX=tableL[i];
		var tableC=tableX.className;
		if (tableC!=null && tableC=="tableList"){
			doubleBgColor(tableX,"#f9f9f9","#ffffff")
			}
	}
}
//隔行变色
function doubleBgColor(Table,Bg1,Bg2) {
    for (var i=0;i<Table.rows.length;i++) Table.rows[i].bgColor=i%2?Bg2:Bg1;
}
//checkAll
function checkAll(status)
{	//alert(this.value);
	var chbox=document.getElementsByName("checkbox");
	var chboxLen=chbox.length;
	for(i=0;i<chboxLen;i++){
		chbox[i].checked=status;
		}
}
function rightCheck(id,status)
{		
	//var checkRight=document.getElementById("checkRight");
	var chbox=document.getElementsByName(id.name);
	var chboxLen=chbox.length;
	for(i=0;i<chboxLen;i++){
		chbox[i].checked=status;
		//checkRight.value=checkRight.value+chbox[i].value+",";
		}

}
function rightCheck2(id,status)
{		
	var checkRight=document.getElementById("insertRole_checkRight");
	var chbox=document.getElementsByName(id.name);
	var chboxLen=chbox.length;
	for(i=0;i<chboxLen;i++){
		chbox[i].checked=status;
		if(status==false){
			var objVal=chbox[i].value+",";
			checkRight.value=checkRight.value.replace(objVal,'');
			
		}
		else{
			var ss = checkRight.value.indexOf(chbox[i].value);
		  	
		  	if(ss<0){
			checkRight.value=checkRight.value+chbox[i].value+",";
			}
		}
		
	}

}
function rightCheck1(obj,status)
{		
	var checkRight=document.getElementById("insertRole_checkRight");
		if(status==false){
			var objVal=obj.value+",";
			checkRight.value=checkRight.value.replace(objVal,'');
			
		}
		else{
			checkRight.value=checkRight.value+obj.value+",";
		}
}

//获取字符串长度
function getLength(str){
     var length = str.length;
     for(var i = 0; i < str.length; i++)
      if(/[\u4e00-\u9fa5]/.test(str.charAt(i)))
       length ++;
     return length;
}

window.onload = function (){
	tableList();
}
function editData(url){
	var checkBox=document.getElementsByName("checkbox");
	var str="";
	for(i=0;i<checkBox.length;i++){
	 if(checkBox[i].checked==true){
	 		if(str==""){
				if(checkBox[i].value!="checkbox"){
					str=str+checkBox[i].value;
				}
	 		}else{
	 			str=str+","+checkBox[i].value;
	 		}
	 	}
	}
	if(str.split(",").length<2 && str!="" ){
		var urlAddress =url+str;
    	window.location.href=urlAddress;
	}
	if(str.split(",").length>1){
		alertMsg("","",23);
		return false;
	}
	if(str==""){
		alertMsg("","",8);
		return false;
	}
}
function delBox(url){
	var checkBox=document.getElementsByName("checkbox");
	var str="";
	for(i=0;i<checkBox.length;i++){		
		if(checkBox[i].checked == true){
			if(str==""){
				if(checkBox[i].value!="checkbox"){
					str=str+checkBox[i].value;
				}
	 		}else{
	 			str=str+","+checkBox[i].value;
	 		}						
		}
	}
	if(str==""){
		alertMsg("","",7);
	}
	else{
		var urlAddress=url+str;		
		infoMsg("",urlAddress,1);
		return false;	
	}
}
//update img
function onUploadImgChange(sender,upLoadMapId,upLoadMapWidth,upLoadMapHeight){
	if( !sender.value.match(/.jpg|.gif|.png|.bmp/i)){//允许的图片格式为.jpg|.gif|.png|.bmp
		 
		var uploadObj=document.getElementById(upLoadMapId);
		var uploadTdObj=document.getElementById("uploadTd");
		var newUpLoad=uploadTdObj.innerHTML;
		//清空之前选择的文件

		uploadTdObj.removeChild(uploadObj);
		uploadTdObj.innerHTML=newUpLoad;
		//清空长宽值

		document.getElementById(upLoadMapWidth).value="";
		document.getElementById(upLoadMapHeight).value=""
		var strTxt="图片格式无效！"
		alertMsg("",strTxt,0);
		return false;
	}
	
	var objPreview = document.getElementById('preview' );
	var objPreviewFake = document.getElementById('preview_fake' );
	var objPreviewSizeFake = document.getElementById('preview_size_fake' );
	if( sender.files &&sender.files[0] ){
	  objPreview.style.display ='block';
	  objPreview.style.width ='auto';
	  objPreview.style.height ='auto';
	 
	  // Firefox 因安全性问题已无法直接通过input[file].value
	  objPreview.src =sender.files[0].getAsDataURL();
	}else if(objPreviewFake.filters){
	  // IE7,IE8 在设置本地图片地址为 img.src 时出现莫名其妙的后果
	  //（相同环境有时能显示，有时不显示），因此只能用滤镜来解决
	 
	  // IE7, IE8因安全性问题已无法直接通过 input[file].value 获取完整的文件路径

	  sender.select();
	  var imgSrc =document.selection.createRange().text;  
	  objPreviewFake.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src= imgSrc;
	  objPreviewSizeFake.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src= imgSrc;  
	  autoSizePreview( objPreviewFake,objPreviewSizeFake.offsetWidth,objPreviewSizeFake.offsetHeight,upLoadMapWidth,upLoadMapHeight);
	  objPreview.style.display ='none';
	}
}


function onPreviewLoad(sender)
{autoSizePreview(sender,sender.offsetWidth, sender.offsetHeight);}


function autoSizePreview( objPre, originalWidth, originalHeight,upLoadMapWidth,upLoadMapHeight){
	var zoomParam = clacImgZoomParam( 100, 100, originalWidth,originalHeight);
	objPre.style.width = zoomParam.width +'px';
	objPre.style.height = zoomParam.height +'px';
	objPre.style.marginTop = zoomParam.top +'px';
	objPre.style.marginLeft = zoomParam.left + 'px';
	var widthMap=document.getElementById(upLoadMapWidth);
	var heightMap=document.getElementById(upLoadMapHeight);
	if(originalWidth!=0&&originalHeight!=0){
		widthMap.value=originalWidth;
		heightMap.value=originalHeight;
		}
}
function clacImgZoomParam( maxWidth, maxHeight, width, height ){
	var param = { width:width, height:height, top:0, left:0 };
	if(width>maxWidth || height>maxHeight ){
	  rateWidth = width /maxWidth;
	  rateHeight = height /maxHeight;  
	  if( rateWidth > rateHeight){
		  param.width =maxWidth;
		  param.height = height /rateWidth;
	  }else{
		  param.width = width /rateHeight;
		  param.height =maxHeight;
	  }
	}
	
	param.left = (maxWidth - param.width) / 2;
	param.top = (maxHeight - param.height) /2;
	return param;
}

var stepToSave = 1;

var OP_ADD = "a";
var OP_MODIFY = "m";
var OP_MOVE	= "v";
var OP_DELETE = "d";

var deviceIcon = {x:30,y:12};
//记录所有已存在的读卡器编号
//数组形式  每次添加修改删除时都进行确定是否重复，并且维护
var lrmID = new Array();
///////////////////////

var count = 0;
var opCount = 0;
var savedCount = 0;
var devices = new Array();
var operations = new Array();
var drag = false;
var dragObject;
var mouseOffset;
var startPosition;
var submitting = false;
var xmlHttp;
var intMapID;



//提交动作
function submit() {
	var date = new Date();
	submitting = true;
	var c = opCount - savedCount;
	var url = "/SCMDB/readerAction!commit.action?date="+date.getTime();
	url += "&count=" + c + "&start=" + savedCount;
	//将每一步的提交信息写成url提交 
	for (i = 0; i < c; i++) {
		var index = savedCount + i;
		var op = operations[index][0];
		url += "&op" + i + "=" + op;
		if (op == OP_MOVE) {
			url += "&id" + i + "=" + operations[index][1];
			url += "&x" + i + "=" + operations[index][2];
			url += "&y" + i + "=" + operations[index][3];
			

		}
		if (op == OP_ADD) {			
			url += "&id" + i + "=" + operations[index][1];
			url += "&type" + i + "=" + operations[index][2];
			url += "&x" + i + "=" + operations[index][3];
			url += "&y" + i + "=" + operations[index][4];
			url += "&security" + i + "=" + operations[index][5];
			url += "&lID" + i + "=" + operations[index][6];
			url += "&wpID" + i + "=" + operations[index][7];
			url += "&strPos" + i + "=" + operations[index][8];
			
		}
		if (op == OP_MODIFY) {
			url += "&id" + i + "=" + operations[index][1];
			url += "&type" + i + "=" + operations[index][2];
			url += "&x" + i + "=" + operations[index][3];
			url += "&y" + i + "=" + operations[index][4];
			url += "&security" + i + "=" + operations[index][5];
			url += "&lID" + i + "=" + operations[index][6];
			url += "&wpID" + i + "=" + operations[index][7];
			url += "&strPos" + i + "=" + operations[index][8];
			
		}
		if (op == OP_DELETE) {
			url += "&id" + i + "=" + operations[index][1];
		}
	}
	savedCount = opCount;
	submitting = false;
	
	xmlHttp = GetXmlHttpObject();
	xmlHttp.open("GET", url, true);	
	//当xml返回时定义handleResponse()为响应函数
	xmlHttp.onreadystatechange = handleResponse;
	xmlHttp.send(null);	
}

//对于ajax的xml返回结果的解析和处理
function handleResponse()
{

	if (xmlHttp.readyState == 4) {
		//alert("修改已提交");
		//alert(xmlHttp.responseText);
		var xmlResponse = xmlHttp.responseXML;
		var  xmldoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
		xmldoc.async = false;
		xmldoc.loadXML(xmlHttp.responseText);
		var status = xmldoc.selectSingleNode("//xml-body/status").text;
		if (status == "success") {
			//此段进行新添加的id的转换  后期改变后  这段已经不会执行  已无实际意义
			var idmap = xmldoc.selectNodes("//xml-body/idmap");
			for (var i = 0; i < idmap.length; i++) {
				var tempid = idmap[i].selectSingleNode("tempid").text;
				var realid = idmap[i].selectSingleNode("realid").text;
				document.getElementById("new" + tempid).id = realid;
				devices[tempid][0] = realid;
			}
		}
		
		submitting = false;
	}
}

function checkSubmit(){

	if (submitting)
		return true;

	if (opCount - savedCount >= stepToSave) {
		submit();
	}
}


//输入该地图上初始的读卡器信息
function preDevice(id, type, x, y,securityLevel,wpID,strPos) {
	devices[count] = new Array();
	devices[count][0] = id;
	devices[count][1] = type;
	devices[count][2] = x;
	devices[count][3] = y;
	devices[count][4] = securityLevel;
	devices[count][5] = id;
	devices[count][6] = wpID;
	devices[count][7] = strPos;
	
	count = count + 1;
}


//添加读卡器
function newDevice(type, security, x, y,lID,wpID,strPos) {
	//检测新添加的读卡器是否已存在
	for ( i=0;i<count;i++) {
		if (lID == devices[i][5]) {
			alert("编号为"+lID+"的读卡器已经存在　　请更换新读卡器的编号")
			return;
		}
	}
	
	var tempId = lID;
	operations[opCount] = new Array();
	operations[opCount][0] = OP_ADD;
	operations[opCount][1] = tempId;
	operations[opCount][2] = type;
	operations[opCount][3] = x;
	operations[opCount][4] = y;
	operations[opCount][5] = security;
	operations[opCount][6] = lID;
	operations[opCount][7] = wpID;
	operations[opCount][8] = strPos;
	opCount = opCount + 1;
	

	
	devices[count] = new Array();
	devices[count][0] = tempId;
	devices[count][1] = type;
	devices[count][2] = x;
	devices[count][3] = y;
	devices[count][4] = security;
	devices[count][5] = lID;
	devices[count][6] = wpID;
	devices[count][7] = strPos;	
	count = count + 1;	

	
	var devElement = document.getElementById("dev");
	var imgElement = document.createElement("img");
	imgElement.setAttribute("src", "/SCMDB/images/reader.gif");
	imgElement.setAttribute("width","16");
	imgElement.setAttribute("height","16");
	imgElement.setAttribute("id", tempId);
	imgElement.setAttribute("title", "读卡器号: " + lID + " 位置：" + strPos);
	devElement.appendChild(imgElement);
	
	var pic = document.getElementById(tempId);
	pic.style.position="absolute";
	pic.style.left = x + "px";
	pic.style.top = y + "px";
	pic.style.setAttribute("z-index", "1");	
	
	
	//添加功能由action来做,这个只负责图形的显示

	checkSubmit();
}


//修改读卡器相关信息
function modifyDevice(id,type, security, x, y,lID,wpID,strPos) {

	//检测修改后的读卡器id号是否同已存在的冲突
	for ( i=0;i<count;i++) {
		if (lID == devices[i][5] && id != devices[i][0]) {
			alert("编号为"+lID+"的读卡器已经存在　　请更换读卡器的编号")
			return;
		}
	}
	
	for ( i=0;i<count;i++) {
		if (id == devices[i][0]) {
			devices[i][1] = type;
			devices[i][2] = x;
			devices[i][3] = y;
			devices[i][4] = security;
			devices[i][5] = lID;
			devices[i][6] = wpID;
			devices[i][7] = strPos;	
		}
	}
	

	operations[opCount] = new Array();
	operations[opCount][0] = OP_MODIFY;
	operations[opCount][1] = id;
	operations[opCount][2] = type;
	operations[opCount][3] = x;
	operations[opCount][4] = y;
	operations[opCount][5] = security;
	operations[opCount][6] = lID;
	operations[opCount][7] = wpID;
	operations[opCount][8] = strPos;
	opCount = opCount + 1;


	var pic = document.getElementById(id);
	pic.title = "读卡器: " + lID + " 位置：" + strPos;
	pic.style.position="absolute";
	pic.style.left = x + "px";
	pic.style.top = y + "px";
	
	//由mapAddLRMAction直接完成
	checkSubmit();

}


//删除读卡器
function deleteDevice(id) {
	for ( i=0;i<count;i++) {
		if (id == devices[i][0]) {
			devices[i][0] = -1;
			devices[i][5] = -1;
		}
	}
	

	operations[opCount] = new Array();
	operations[opCount][0] = OP_DELETE;
	operations[opCount][1] = id;
	opCount = opCount + 1;

	
	
	var pic = document.getElementById(id);
	pic.style.position="absolute";
	pic.style.visibility = "hidden";
	document.getElementById(id).parentNode.removeChild(document.getElementById(id))
	checkSubmit();
}

//移动读卡器
function moveDevice(id, x, y) {
	operations[opCount] = new Array();
	operations[opCount][0] = OP_MOVE;
	operations[opCount][1] = id;
	operations[opCount][2] = x;
	operations[opCount][3] = y;
	opCount = opCount + 1;
	checkSubmit();
}

//鼠标点击事件的处理
function mouseDown(ev) {
	if (!isLeftButton(ev))	
		return true;				

	var target = getTarget(ev);
	if (!target || !target.id)
		return true;
	var id = target.id;
	
	if (id == "map")
		return true;
	
	//判断是否为拖拽行为
	mouseOffset = getMouseOffset(target, ev);
	startPosition = getElementPosition(target);

	dragObject = target;	
	drag = true;
	
	return false;	
}

//鼠标移动
function mouseMove(ev) {
	
	if (drag) {
		var mousePosition = getMousePosition(ev);
		dragObject.style.left = mousePosition.x - mouseOffset.x;
		dragObject.style.top = mousePosition.y - mouseOffset.y;		
		
		return false;
	}
}

//鼠标按键抬起动作
function mouseUp(ev) {
	if (drag) {
		if (dragObject.style.left!= startPosition.x + "px" ||
			dragObject.style.top != startPosition.y + "px") {			
			if (!isIn(ev, document.getElementById("map"))) {
				dragObject.style.left = startPosition.x + "px";
				dragObject.style.top = startPosition.y + "px";	
			}else {
				moveDevice(dragObject.id, 
					parseInt(dragObject.style.left), 
					parseInt(dragObject.style.top)
				);
			}
		}
		drag = false;	
		return false;
	}
}

//鼠标双击动作
function dblClick(ev) {

	if (drag)
		drag = false;

	var target = getTarget(ev);
	if (!target || !target.id)
		return true;
		
	var mousePosition = getMousePosition(ev);
	var url;
		
	//在地图上双击为添加读卡器
	if (target.id == "map") {
		var x = mousePosition.x - deviceIcon.x;
		var y = mousePosition.y - deviceIcon.y;	

		url = "/SCMDB/reader/readerNew.jsp?op=add&x=" + x + "&y=" + y;
		window.open(url, "", "width=500px, height=300px, resizable");
	}
	//双击设备 即 修改设备
	for ( i=0;i<count;i++) {
		if (target.id == devices[i][0]) {
			var x = mousePosition.x - deviceIcon.x;
			var y = mousePosition.y - deviceIcon.y;
			var id = devices[i][5];
			var t_security = devices[i][4];
			var t_wp = devices[i][6];
			url = "/SCMDB/reader/readerEdit.jsp?op=update&id="+target.id+"&lID="+id+"&security="+t_security+"&wp="+t_wp+"&x=" + x + "&y=" + y;

			window.open(url, "", "width=500px, height=300px, resizable");
		
		}			
	}
}

//Just for netscape navigator
function MM_reloadPage(init) {  
	//reloads the window if Nav4 resized
	if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
		document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
	else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

function GetXmlHttpObject()
{ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest)
		objXMLHttp=new XMLHttpRequest();
	else if (window.ActiveXObject)
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	return objXMLHttp;
}

//判断按下的是否鼠标左键
function isLeftButton(ev) {
	ev = ev || window.event;
	if (ev.button == 1)
		return true;
	else
		return false;
}

//获取鼠标位置处的对象
function getTarget(ev) {
	ev = ev || window.event;
	return ev.srcElement;
}

//获取鼠标相对浏览器客户区的偏移位置
function getMousePosition(ev) {
	ev = ev || window.event;
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

//获取指定对象相对浏览器客户区的偏移位置
function getElementPosition(e){
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
}

//获取鼠标相对于指定对象的偏移位置
function getMouseOffset(target, ev){
	var docPos    = getElementPosition(target);
	var mousePos  = getMousePosition(ev);
	
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}

//判断鼠标是否位于某一对象处
function isIn(ev, target) {
	var docPos = getElementPosition(target);
	var mousePos = getMousePosition(ev);
	
	if (mousePos.x < docPos.x || mousePos.x > docPos.x + target.width)
		return false;
		
	if (mousePos.y < docPos.y || mousePos.y > docPos.y + target.height)
		return false;
	
	return true;
}